class Solution {
public:
    bool canMake(vector<int>&bloomDay,int m,int k,int days){
        int group=0;
        int flowers=0;
        int n=bloomDay.size();
        for(int i=0;i<n&group<m;++i){
            if(bloomDay[i]<=days){
                flowers++;
                if(flowers==k){
                    group++;
                    flowers=0;
                }
            }else{
                flowers=0;
            }
        }
        return group>=m;
    }
    int minDays(vector<int>& bloomDay, int m, int k) {
        int h=0;
        int l=INT_MAX;
        int n=bloomDay.size();
        if(m>n/k){
            return -1;
        }
        for(int i=0;i<n;i++){
            l=min(l,bloomDay[i]);
            h=max(h,bloomDay[i]);
        }
        while(l<h){
            int days=l+(h-l)/2;
            if(canMake(bloomDay,m,k,days)){
                h=days;
            }else{
                l=days +1;
            }
        }
        return l;
    }
};
